package com.example.learn.juc.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 王明
 * @date 2025/11/3 15:00
 * @description:重入锁测试例子
 */
public class ReentrantLockDemo2 {

    public static void main(String[] args) throws InterruptedException {
        // 创建计数器对象
        Counter counter = new Counter();

        // 测试递归调用
        counter.recursiveCall(10);
    }

}

class Counter {
    // 创建 ReentrantLock 对象
    private final ReentrantLock lock = new ReentrantLock();
    // 计数器
    private final int count = 0;


    public void recursiveCall(int num) {
        // 获取锁
        lock.lock();
        try {
            if (num == 0) {
                return;
            }
            System.out.println("执行递归，num = " + num);
            recursiveCall(num - 1);
        } finally {
            // 释放锁
            lock.unlock();
        }
    }
}